/**
 * @FileName: VnetService.java
 * @Author
 * @Description:
 * @Date 2016年9月1日 上午10:52:37
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.service;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.quartz.core.QuartzScheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.model.TariffClass;
import cn.hpclub.server.util.InterfaceUtil;

public class TariffClassService{
    private static final Logger      logger  = LoggerFactory.getLogger(QuartzScheduler.class);
    public static TariffClassService service = new TariffClassService();

    public String getSqlExceptSelect(ConcurrentHashMap<String, String> searchInfo){
        String sqlExceptSelect = " FROM tariff_class t " + " LEFT JOIN sys_user u ON u.id = t.created_by "
                + " LEFT JOIN organization j ON j.id = t.org_id "
                + " LEFT JOIN activity_tariff_class_relation acr ON t.id=acr.tariff_class_id "
                + " LEFT JOIN activity a ON acr.activity_id=a.id"
                + " LEFT JOIN sys_code_value sv ON a.type = sv.`value` "
                + " LEFT JOIN sys_code_type st ON sv.type_id=st.id "
                + " WHERE t.is_deleted = 0  AND (st.code='HDLX' or st.code IS NULL)";

        for(Entry<String, String> item : searchInfo.entrySet()){
            sqlExceptSelect += buildQuery(item);
        }

        sqlExceptSelect += " GROUP BY t.id ORDER BY last_updated_time DESC,sort ASC";

        logger.debug("sqlExceptSelect = " + sqlExceptSelect);
        return sqlExceptSelect;
    }

    public List<Entry<String, String>> getActivityList(){
        List<Entry<String, String>> list = new ArrayList<Entry<String, String>>();
        List<Record> recordList = Db.find("SELECT a.id, concat(sv.name,'-',a.name) as name" + " FROM activity a "
                + "LEFT JOIN sys_code_value sv ON a.type=sv.`value` "
                + "LEFT JOIN sys_code_type st ON sv.type_id=st.id "
                + "WHERE a.is_deleted = 0 AND st.code='HDLX' ORDER BY a.sort ASC ");
        for(Record record : recordList){
            list.add(new AbstractMap.SimpleEntry<String, String>(record.getStr("name"),
                    String.valueOf(record.getLong("id"))));
        }
        return list;
    }

    private String buildQuery(Entry<String, String> item){
        String sql = "";
        if(item.getKey().equals("name")){
            // 办理人姓名
            sql += " and t.name like '%" + item.getValue() + "%' ";
        } else if(item.getKey().equals("activity_selected") && !"-1".equals(item.getValue())){
            sql += " AND t.id IN(SELECT tariff_class_id FROM activity_tariff_class_relation WHERE activity_id="
                    + item.getValue() + ") ";
        } else if(item.getKey().equals("enabled_selected") && !"-1".equals(item.getValue())){
            sql += " AND t.enabled = " + item.getValue() + " ";
        } else if(item.getKey().equals("org_id")){
            sql += " and t.org_id in("
                    + InterfaceUtil.getChlidOrgIdSql(Integer.parseInt(item.getValue()), Constant.MAX_ORG_LEVEL, true)
                    + ") ";
        }
        return sql;
    }

    public boolean getTariffByCityAndCode(int city_org_id, String tariffClassName, int tariffClassId){
        StringBuffer sql = new StringBuffer("SELECT * FROM tariff_class WHERE name=? AND is_deleted=0 ");
        if(tariffClassId > 0){
            sql.append(" AND id !=" + tariffClassId);
        }
        sql.append(" AND  org_id = ").append(city_org_id).append(" ");
        List<Record> tariffClassList = Db.find(sql.toString(), tariffClassName);
        return tariffClassList.size() > 0 ? true : false;
    }

    /**
     * @Title: getTariffClassByActType
     * @Description:TODO
     * @param actType
     * @return List<Record>
     */
    public List<Record> getTariffClassByActType(String actType){
        return TariffClass.dao.getTariffClassByActType(actType);
    }

}
